import tensorflow as tf
from matplotlib import pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

tf.logging.set_verbosity(tf.logging.INFO)

mnist = input_data.read_data_sets('.')

plt.figure(figsize=(8, 8))

for idx in range(16):
    plt.subplot(4, 4, idx + 1)
    plt.axis('off')
    plt.title('[{}]'.format(mnist.train.labels[idx]))
    plt.imshow(mnist.train.images[idx].reshape((28, 28)))

plt.show()

learning_rate = tf.placeholder(tf.float32)

x = tf.placeholder("float", [None, 784])
y = tf.placeholder("int64", [None])


def initialize(shape, stddev=0.1):
    return tf.truncated_normal(shape, stddev=0.1)


L1_units_count = 200
W_1 = tf.Variable(initialize([784, L1_units_count]))
b_1 = tf.Variable(tf.zeros([L1_units_count]), name='bias')
logits_1 = tf.matmul(x, W_1) + b_1
output_1 = tf.nn.relu(logits_1)

L2_units_count = 200
W_2 = tf.Variable(initialize([L1_units_count, L2_units_count]))
b_2 = tf.Variable(tf.zeros([L2_units_count]), name='bias')
logits_2 = tf.matmul(output_1, W_2) + b_2
output_2 = tf.nn.relu(logits_2)

L3_units_count = 200
W_3 = tf.Variable(initialize([L2_units_count,L3_units_count]))
b_3 = tf.Variable(tf.zeros([L3_units_count]), name='bias')
logits_3 = tf.matmul(output_2, W_3) + b_3
output_3 = tf.nn.relu(logits_3)

L4_units_count = 200
W_4 = tf.Variable(initialize([L3_units_count,L4_units_count]))
b_4 = tf.Variable(tf.zeros([L4_units_count]), name='bias')
logits_4 = tf.matmul(output_3, W_4) + b_4
output_4 = tf.nn.relu(logits_4)

L5_units_count = 10
W_5 = tf.Variable(initialize([L4_units_count,L5_units_count]))
b_5 = tf.Variable(tf.zeros([L5_units_count]), name='bias')
logits_5 = tf.matmul(output_4, W_5) + b_5

logits = logits_5

cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y))
optimizer = tf.train.GradientDescentOptimizer(
    learning_rate=learning_rate).minimize(cross_entropy)
pred = tf.nn.softmax(logits)
correct_prediction = tf.equal(tf.argmax(pred, 1), y)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

batch_size = 32
trainig_step = 30000

saver = tf.train.Saver()

sess = tf.Session()
sess.run(tf.global_variables_initializer())

lr = [0.5,0.4,0.3,0.2,0.1]

validate_data = {
    x: mnist.validation.images,
    y: mnist.validation.labels,
}
test_data = {x: mnist.test.images, y: mnist.test.labels}

for step in range(trainig_step):
    batch_x, batch_y = mnist.train.next_batch(batch_size)
    _, loss = sess.run([optimizer, cross_entropy],
                       feed_dict={x: batch_x,
                                  y: batch_y,
                                  learning_rate: lr[int(step/(trainig_step/len(lr)))]})
    if (step + 1) % 100 == 0:
        print("#" * 10)
        print('learning rate: [{}],entropy loss: [{}]'.format(lr[int(step/(trainig_step/len(lr)))],loss))
        print(sess.run(accuracy, feed_dict={x: batch_x, y: batch_y}))
        print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))

